$(function(){
    var obj = {
        redirect: $.getQueryString('redirect'),
        userTel: '',
        userTelIpt: '',
        yzm: '',
        djs: 0,
        timer: null,
        ajaxStatus: false,
        init: function () {
            var that = this;
            that.userTel = localStorage.getItem('myTel') || '';
            that.addEvent();
        },
        addEvent: function () {
            var that = this;
            //取消
            $('#cancel').click(function () {
                history.go(-1);
            });
            // 注册
            $('#register').click(function () {
                location.href = './mobileRegister.html?redirect=1';
            });
            //登录
            $('#login').click(function () {
                that.userTelIpt = $('#tel').val();
                that.yzm = $('#yzm').val();
                if(!that.userTelIpt) $.alert('请输入手机号码');
                else if(isNaN(that.userTelIpt)) $.alert('请输入正确的手机号');
                else if(!that.yzm) $.alert('请填写验证码');
                else that.ajaxLoginIn(); //登录
            });
            //验证码
            $('.btn-yzm').click(function () {
                that.userTelIpt = $('#tel').val();
                if(!that.userTelIpt) $.alert('请输入手机号码');
                else if(isNaN(that.userTelIpt)) $.alert('请输入正确的手机号');
                else {
                    // 倒计时结束重发验证码
                    if(that.djs === 0){
                        that.resetDJS();
                        that.ajaxGetYZM();
                    }else{
                        // 倒计时未结束不重发
                    }
                }
            })
        },
        // 获取验证码
        ajaxGetYZM: function() {
            var that = this;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getYZM',
                type: 'get',
                contentType: 'application/json',
                data: {
                    tel:encodeURI(that.userTelIpt),
                },
                success: function (res) {
                    $.alert('验证码发送成功')
                },
                error: function (res) {
                }
            })
        },
        ajaxLoginIn: function(){
            var that = this;
            if(that.ajaxStatus) return;
            that.ajaxStatus = true;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getLogin',
                type: 'post',
                contentType: 'application/json',
                data: {
                    tel: that.userTelIpt,
                    yzm: that.yzm
                },
                success: function (res) {
                    // 成功后跳转
                    that.ajaxStatus = false;
                    clearInterval(that.timer);
                    that.userTel = that.userTelIpt;
                    localStorage.setItem('myTel',that.userTelIpt);
                    $.alert('登录成功',1000,function () {
                        if(that.redirect == 1||history.length<=1) window.location.replace('./jobSearchList.html');
                        else history.go(-1);
                    });
                },
                error: function (res) {
                    that.ajaxStatus = false;
                }
            })
        },
        // 重置倒计时
        resetDJS:function () {
            var that = this;
            clearInterval(that.timer);
            that.djs = 30;
            $('.btn-yzm').html(that.djs+'s后重试');
            that.timer = setInterval(function () {
                if(that.djs>0){
                    that.djs --;
                    $('.btn-yzm').html(that.djs+'s后重试');
                }else{
                    $('.btn-yzm').html('重发验证码');
                    clearInterval(that.timer);
                }
            },1000);
        }
    };
    obj.init();
});